package com.atguigu.guli.service.edu.controller.api;


import com.atguigu.guli.service.bean.eduBean.CrouseBean;
import com.atguigu.guli.service.edu.entity.Chapter;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.form.CourseQuery;
import com.atguigu.guli.service.edu.entity.form.CourseQueryVo;
import com.atguigu.guli.service.edu.service.ChapterService;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.result.R;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-06-23
 */
@RestController
@RequestMapping("/api/edu/course")
public class ApiCourseController {
    @Autowired
    CourseService courseService;
    @Autowired
    ChapterService chapterService;


    // 远程访问,根据id查询课程   还要查到讲师姓名
    @GetMapping("/getCourseFromTrade/{courseId}")
    public R getCourseFromTrade(@PathVariable("courseId") String courseId){
       CrouseBean course = courseService.getCourseFromTrade(courseId);
        return R.ok().data("course", course);
    }

    // 远程访问,根据id查询课程,就销售数据进行修改
    @PutMapping("/update/{courserId}")
    public R update(@PathVariable String courserId){
        Course course = courseService.getById(courserId);
        UpdateWrapper<Course> wrapper= new UpdateWrapper<>();
        wrapper.eq("buy_count",course.getBuyCount()+1);
        courseService.update(wrapper);
        return R.ok();
    }


    @GetMapping("/getCrouse/{teacherId}") //通过讲师id查询课
    public R getCrouse(@PathVariable String teacherId){
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        //只查询这三个属性
        wrapper.select("title","cover","id");
        wrapper.eq("teacher_id", teacherId);
        //排序
        wrapper.orderByDesc("gmt_create");
        List<Course> courses = courseService.list(wrapper);
        return R.ok().data("items",courses);
    }

    //根据条件进行查询课时
    @GetMapping("/getCrouseQueryAll")
    public R getCrouseQueryAll(CourseQuery courseQuery){
        //根据条件查询
       List<Course> courses = courseService.getCrouseQueryAll(courseQuery);
       return R.ok().data("items",courses);
    }

    //多表查询,查出多个值,进行页面渲染, 根据课程的id
    @GetMapping("/getCourseAllMcg/{courseId}")
    public R getCourseAllMcg(@PathVariable String courseId){
       CourseQueryVo courseQueryVoList = courseService.getCourseAllMcg(courseId);
       return R.ok().data("item",courseQueryVoList);

    }

    @GetMapping("/getChapterList/{courseId}")  //查询课时
    public R getChapterList(@PathVariable String courseId){
        List<Chapter> list = chapterService.getChapterAndVideo(courseId);
        return R.ok().data("items",list);

    }



}

